﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace AgvDemo
{

    

    public class JigouClass
    {
        public string JigouName { get; set; }

        public int AgvNum { get; set; }
        /// <summary>
        /// Agv编号
        /// </summary>
        public DateTime AllowTime { get; set; }

        /// <summary>
        /// 目标工位
        /// </summary>
        //public DateTime ErrorDate { get; set; }
    }

    public class AgvInfoClass
    {
        public int AgvNum { get; set; }

        public string AgvIp { get; set; }
        /// <summary>
        /// Agv编号
        /// </summary>
        public int AgvLine { get; set; }
    }


    public class ErrorClass
    {
        public int ErrorAgv { get; set; }

        public int ErrorStation { get; set; }
        /// <summary>
        /// Agv编号
        /// </summary>
        public int ErrorCode { get; set; }

        /// <summary>
        /// 目标工位
        /// </summary>
        public DateTime ErrorDate { get; set; }
    }

    public class SpeciaTraffic
    {
        /// <summary>
        /// Agv编号
        /// </summary>
        public int AgvLockCard { get; set; }
     
        /// <summary>
        /// 目标工位
        /// </summary>
        public int AgvNo { get; set; }
    }

    public class AgvTaskState
    {
        /// <summary>
        /// Agv编号
        /// </summary>
        public int AgvNum { get; set; }

        /// <summary>
        /// 目标工位
        /// </summary>
        public int CodeNo { get; set; }

        public bool flag { get; set; }
    }


    public class AgvCommandClass
    {
        /// <summary>
        /// Agv编号
        /// </summary>
        public int AgvNum { get; set; }

        public int CurrentStation { get; set; }

        /// <summary>
        /// 目标工位
        /// </summary>
        public string CommandName { get; set; }


       

        public string CurrentTime { get; set; }

       
    }

    public class AgvChargeClass
    {
        /// <summary>
        /// Agv编号
        /// </summary>
        public int ChargeNum { get; set; }

        /// <summary>
        /// 目标工位
        /// </summary>
        public int ChargeAgv { get; set; }

        public bool ChargeState { get; set; }

        public System.Windows.Forms.Timer tmCharge{ get; set; }
        public int Count { get; set; }


        public bool ChargeControl { get; set; }

        public int  maxCount { get; set; }
    }


    public class AgvStationClass
    {
        /// <summary>
        /// Agv编号
        /// </summary>
        public int AgvNum { get; set; }

        /// <summary>
        /// 目标工位
        /// </summary>
        public int Station { get; set; }

        public string CurrentTime { get; set; }


    }





    public class AgvFunctionClass
    {
        private static AgvFunctionClass uniqueInstance;

        public static List<AgvInfoClass> agvInfosList = new List<AgvInfoClass>();


        public static List<AgvStationClass> stationRecordList = new List<AgvStationClass>();

        public static string TotalFlag = string.Format("{0:yyyyMMdd}", DateTime.Now);

        public static string agv9Ip;
        public static string agv10Ip;
        public static string agv11Ip;
        public static string agv12Ip;

        public static int VerticalScrollIndex = 0;
        public static int HorizontalOffset = 0;

        public static int VerticalScrollIndex1 = 0;
        public static int HorizontalOffset1 = 0;

        public static int VerticalScrollIndex2 = 0;
        public static int HorizontalOffset2 = 0;

        public static List<PlcManagerClass> plcMList = new List<PlcManagerClass>();

        public static List<AgvCommandClass> commandList = new List<AgvCommandClass>();


        public static Dictionary<int, bool> ShowBoolDic = new Dictionary<int, bool>();

        public static Dictionary<int, bool> ErrorPathDic = new Dictionary<int, bool>();

        public static int agv9Num=0;
        public static int agv10Num=0;
        public static int agv11Num=0;
        public static int agv12Num=0;

        public static int pingTime = 0;


     

        public static List<JigouClass> jigouList = new List<JigouClass>();

       // public bool disPatchControl;
        public bool chargeDispatchControl;
        public static List<ErrorClass> errorClassList = new List<ErrorClass>();
        public Dictionary<string, AgvPublic.ClientTcp> ipClient = new Dictionary<string, AgvPublic.ClientTcp>();


        public static Dictionary<int, int> WaitDic = new Dictionary<int, int>();

        private AgvFunctionClass()
        {
            _ini = new IniFile.IniFile(Application.StartupPath + @"\config.ini");
           


            //GetMapList();
           // GetSystemMode();
           
            ArriveDic = new Dictionary<int, int>();
          
            RecordDic = new Dictionary<int, int>();
            LevelDic = new Dictionary<int, int>();
            TrafficDic = new Dictionary<int, int>();
            AgvOldStationDic = new Dictionary<int, int>();
            HeartbeatDic = new Dictionary<string, DateTime>();
            //IpAndAgvinfoDic = new Dictionary<string, AgvPublic.Model.Agv_Info>();
            AgvStateDic = new Dictionary<int, int>();
            RoadDic = new Dictionary<int, int>();
            AgvChargeDic = new Dictionary<int, int>();
            AgvErrorDic = new Dictionary<int, int>();
            //RunStateDic = new Dictionary<int, int>();
            //agvCountDic = new Dictionary<int, int>();
            chanxianTaskDic = new Dictionary<int, int>();
            TrafficSureDic=new Dictionary<int, int>();  
            kuquTaskDic = new Dictionary<int, int>();
            //disPatchControl = false;
            chargeDispatchControl = false;

          
          

           RoadDic.Add(1, 0);
           RoadDic.Add(2, 0);

            WaitDic.Add(1, 0);
            WaitDic.Add(2, 0);



            
           
           TrafficSureDic.Add(1,0);
            TrafficSureDic.Add(2, 0);
            //{
            //    TrafficDic.Add(i, 0);
            //    AgvOldStationDic.Add(i, 0);
            //    AgvStateDic.Add(i, 0);
            //    RecordDic.Add(i, 0);
            //    LevelDic.Add(i, 0);
            //    ArriveDic.Add(i, 0);
            //    AgvChargeDic.Add(i, 0);

            //    AgvErrorDic.Add(i, 0);
            //    //RunStateDic.Add(i, 0);


            //    //if (!ShowDic.ContainsKey(i))
            //    //{

            //    //    ShowBoolDic.Add(i, false);

            //    //    ErrorPathDic.Add(i, false);
            //    //}
            //}

            //errorClassList = GetFaultData();



            //for (int i = 1; i < 31; i++)
            //{
            //    AgvCommandClass acc = new AgvCommandClass();
            //    acc.AgvNum = 3;
            //    acc.CommandName = "启动";
            //    acc.CurrentTime = DateTime.Now.ToString();
            //    commandList.Add(acc);
            //    //RunStateDic.Add(i, 0);
            //}
        }


        public static string GetLineNum()
         {
        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
        return st.GetFrame(0).GetMethod().ReflectedType.FullName + "    Line：" + st.GetFrame(0).GetFileLineNumber().ToString();
         }


    
        public static int chan1Size = 1;
        public static int chan2Size = 1;
        public static int chan3Size = 1;
        public static int okChan3Size = 1;

        public static object lockTaskPlc = new object();//防止下位机数据处理异常
       

        public static bool chan1Start = true;
        public static bool chan2Start = true;
        public static bool chan3Start = true;
       // public Dictionary<int, int> RunStateDic { get; set; } //交管状态 1锁 2可解

        public Dictionary<int, int> ArriveDic { get; set; } //交管状态 1锁 2可解
        public static Dictionary<int,int> TrafficDic { get; set; } //交管状态 1锁 2可解
        public static Dictionary<int, int> AgvOldStationDic { get; set; } //记录上一次站点

        public static Dictionary<int, int> AgvErrorDic { get; set; } //AGV运行状态
        public Dictionary<string, DateTime> HeartbeatDic{ get; set; } //此处通过心跳包来判断是否掉线   
        //public Dictionary<string, AgvPublic.Model.Agv_Info> IpAndAgvinfoDic { get; set; }
        public Dictionary<int, int> AgvStateDic { get; set; } //AGV运行状态
        public Dictionary<int, string> AgvIpDic { get; set; }
        public Dictionary<string, byte[]> bufferDic { get; set; }
        public IniFile.IniFile _ini{ get; set; }
        public DataSet dsSpecial = new DataSet();
        public Dictionary<int, int> RecordDic { get; set; }
        public Dictionary<int, int> LevelDic { get; set; }
        public List<SpeciaTraffic> SpeciaT { get; set; }
        public List<SpeciaTraffic> SpeciaW { get; set; }
        public static List<string> PlcName { get; set; }

        public Dictionary<int, int> AgvChargeDic { get; set; } //AGV运行状态
       
        public Dictionary<int, int> RoadDic { get; set; }

        public static Dictionary<int, int> agvCountDic { get; set; }

        public bool trafficState50 = false;

        public bool trafficState51 = false;

        // public List<AgvPublic.StationMapXyClass> mapList { get; set; }
        // public List<AgvPublic.TrafficClass> areaDetailModelList { get; set; }
        public static bool IsStartTraffic = false;//默认关闭
       

        public int agv1Dir=0;
        public int agv2Dir=0;
        public int agv3Dir=0;

        public static Dictionary<int, int> chanxianTaskDic { get; set; }
        public static Dictionary<int, int> kuquTaskDic { get; set; }



        public static Dictionary<int, int> TrafficSureDic { get; set; }


        public static AgvFunctionClass GetInstance()
        {

            if (uniqueInstance == null)
            {
                uniqueInstance = new AgvFunctionClass();
            }

            return uniqueInstance;
        }

        //public int GetTaskState(int agvNum)
        //{
        //    int x = 0;
        //    for (int i = 0; i < agvActionList.Count; i++)
        //    {
        //        if (agvActionList[i].AgvNum == agvNum)
        //        {
        //            x = agvActionList[i].AgvTaskState;
        //            break;
        //        }
        //    }

        //    return x;
        //}

        //public bool IsExistTaskAgv(int agvNum,int num)
        //{
        //    bool x = false;
        //    for (int i = 0; i < agvActionList.Count; i++)
        //    {
        //        if (agvActionList[i].AgvNum != agvNum && agvActionList[i].AgvTaskState==num)
        //        {
        //            x = true;
        //            break;
        //        }
        //    }

        //    return x;
        //}
        public static bool IsExistKuAgv(int agvNum)
        {
            bool x = false;
            foreach (int road in kuquTaskDic.Keys)
            {
                if (kuquTaskDic[road] == agvNum)
                {
                    x = true;
                    break;
                }
            }
            return x;
        }

        public static bool IsExistChanAgv(int agvNum)
        {
            bool x = false;
            foreach (int road in chanxianTaskDic.Keys)
            {
                if (chanxianTaskDic[road] == agvNum)
                {
                    x = true;
                    break;
                }
            }


            return x;
        }


        public static bool IsExistKongAgv()
        {
            bool x = false;
            foreach (int road in chanxianTaskDic.Keys)
            {
                if (chanxianTaskDic[road] == 0)
                {
                    x = true;
                    break;
                }
            }


            return x;
        }

        public static bool IsExistKuTaskAgv(int agvNum,int station)
        {
            bool x = false;
            foreach (int road in kuquTaskDic.Keys)
            {
                if (kuquTaskDic[road] == agvNum && road == station)
                {
                    x = true;
                    break;
                }
            }
         
            return x;
        }



        public static bool IsExistKuTask25(int agvNum)
        {
            bool x = false;
            if (AgvFunctionClass.kuquTaskDic.ContainsKey(25))
            {
                if (kuquTaskDic[25] != 0 && kuquTaskDic[25] != agvNum)
                {
                    x = true;
                }
            }

            return x;
        }

        public static bool IsExistChanTaskAgv(int agvNum, int station)
        {
            bool x = false;
            foreach (int road in chanxianTaskDic.Keys)
            {
                if (chanxianTaskDic[road] == agvNum && road == station)
                {
                    x = true;
                    break;
                }
            }


            return x;
        }


    

        public static bool IsExistChanXianTaskAgv(int agvNum, int station)
        {
            bool x = false;
            foreach (int road in chanxianTaskDic.Keys)
            {
                if (chanxianTaskDic[road] != agvNum && chanxianTaskDic[road] != 0 && road == station) 
                {
                    x = true;
                    break;
                }
            }
            return x;
        }

        public static bool IsExistChanSingleTaskAgv(int station)
        {
            bool x = false;
            foreach (int road in chanxianTaskDic.Keys)
            {
                if (chanxianTaskDic[road] == 0 && road == station)
                {
                    x = true;
                    break;
                }
            }


            return x;
        }
		public static bool IsExistTaskAgv(int agvNum, int station)
        {
            bool x = false;
            foreach (int road in chanxianTaskDic.Keys)
            {
                if (chanxianTaskDic[road] == 0 || chanxianTaskDic[road] == agvNum)
                {
                    if (road == station)
                    {
                        x = true;
                        break;
                    }
                }
               
            }


            return x;
        }
       


        public void WriteInMapConfig(List<AgvPublic.StationMapXyClass> list)
        {
            try
            {
                if (list.Count != 0)
                {
                    string tempStr = null;

                    foreach (AgvPublic.StationMapXyClass smxc in list)
                    {
                        string temp = smxc.Station_Id + "-" + smxc.Station_X + "-" + smxc.Station_Y;
                        tempStr += temp + "|";
                    }

                    _ini.IniWriteValue("StationXy", "value", tempStr);
                }
                else
                {
                    _ini.IniWriteValue("StationXy", "value", "0");
                }
            }
            catch
            {
            }
        }



        static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        public static string GetRandomNumberOther(int minimum, int maximum)   //Len小数点保留位数
        {

            Random random = new Random(GetRandomSeed());
            int b = random.Next(minimum,maximum);
            return b.ToString();
            //Random ran_int = new Random();
            //Random ran_float = new Random();
            //int ints = ran_int.Next((int)minimum, (int)maximum);
            //double b = ran_float.NextDouble();
            //return (ints + b).ToString("F1");
        }

        //public void GetMapList()
        //{

        //    //mapList = new List<AgvPublic.StationMapXyClass>();
        //    //string value = _ini.IniReadValue("StationXy", "value").Trim();
        //    //if (value != "0" && value != null && value != "")
        //    //{
        //    //    string[] stationStr = value.Split('|');
        //    //    for (int i = 0; i < stationStr.Length; i++)
        //    //    {
        //    //        if (stationStr[i] != "")
        //    //        {
        //    //            string[] tempStr = stationStr[i].Split('-');
        //    //            AgvPublic.StationMapXyClass tempXy = new AgvPublic.StationMapXyClass();
        //    //            tempXy.Station_Id = int.Parse(tempStr[0]);
        //    //            tempXy.Station_X = int.Parse(tempStr[1]);
        //    //            tempXy.Station_Y = int.Parse(tempStr[2]);
        //    //            mapList.Add(tempXy);

        //    //        }

        //    //    }
        //    //}



        //}






    }
}
